package com.mohe.shanpao.netty;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class MessageEncoder extends MessageToByteEncoder<Message> {

	@Override
	protected void encode(ChannelHandlerContext ctx, Message msg, ByteBuf out) throws Exception {
		out.writeLong(msg.getTimestamp());
		String data = msg.getValue();
		if(data == null || data.length() == 0)
		{
			return;
		}
		else
		{
			byte[] content = data.getBytes();
			out.writeInt(content.length);
			out.writeBytes(content);
		}
	}

}
